package com.sign2024.sign2024project.capitalconstruction.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sign2024.sign2024core.common.CommonResult;
import com.sign2024.sign2024core.common.annotation.Log;
import com.sign2024.sign2024core.common.constants.CommonConstants;
import com.sign2024.sign2024core.common.exeception.CommonException;
import com.sign2024.sign2024core.common.utils.ExcelStyleUtils;
import com.sign2024.sign2024core.common.utils.StringUtils;
import com.sign2024.sign2024core.entry.capitalconstruction.JjProgect;
import com.sign2024.sign2024core.entry.projecttask.ProjectTask;
import com.sign2024.sign2024project.capitalconstruction.controller.ExcelType.JJProgectWriteHandler;
import com.sign2024.sign2024project.capitalconstruction.controller.ExcelType.JJProgectWriteHandlerColour;
import com.sign2024.sign2024project.capitalconstruction.param.JjParam;
import com.sign2024.sign2024project.capitalconstruction.service.CreateJJProgect;
import com.sign2024.sign2024project.capitalconstruction.vo.JjProgectVo;
import com.sign2024.sign2024project.projecttask.service.ProjectTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author yujie
 * @data 2024/8/16 11:35
 */

@Api(tags = "基建")
@RestController
@RequestMapping("/api/jJProgect")
public class CreateJJProgectController {

    @Resource
    private CreateJJProgect createJJProgect;

    @Resource
    private ProjectTaskService projectTaskService;

    @Log(title = "基建", businessType = "添加")
    @ApiOperation(value = "生成基建抽取方案", notes = "生成基建抽取方案")
    @Transactional
    @PostMapping("/save")
    public CommonResult save(@RequestParam @ApiParam("任务id") Integer taskId) {
        // 删除之前的数据
        createJJProgect.deleteByTaskId(taskId);
        // 添加数据
        createJJProgect.processingGcDate(taskId);
        // 任务完成  enable_flag
        ProjectTask task = new ProjectTask();
        task.setId(taskId);
        task.setEnableFlag("已完成");
        projectTaskService.updateTask(task);
        return CommonResult.success();
    }

    @Log(title = "基建", businessType = "更新")
    @ApiOperation(value = "修改基建抽取方案信息", notes = "修改基建抽取方案信息")
    @PostMapping("/update")
    public CommonResult<JjProgect> update(JjProgect jjProgect) {
        return CommonResult.success(createJJProgect.updateJJProgect(jjProgect));
    }

    @Log(title = "基建", businessType = "查询")
    @ApiOperation(value = "分页查询基建抽取方案", notes = "分页查询基建抽取方案")
    @PostMapping("/page")
    public CommonResult<JjProgect> page(JjParam param) {
        Page<JjProgect> page = createJJProgect.pageJjProgect(param);
        return CommonResult.success(page);
    }


    @Log(title = "基建", businessType = "导出")
    @ApiOperation(value = "导出基建抽取方案", notes = "导出基建抽取方案")
    @PostMapping("/export")
    public void exportInfo(HttpServletResponse response, Integer taskId) {
        List<JjProgectVo> list = createJJProgect.selectJjProgectVo(taskId);

        // 根据 id 获取 任务信息
        ProjectTask projectTask = projectTaskService.selectProjectTaskData(taskId);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = null;
        try {

            String infoName = "基建专家抽取方案";
            if (StringUtils.isNotNull(projectTask.getBatchNumber())) {
                infoName += "-" + projectTask.getBatchNumber();
            }

            fileName = URLEncoder.encode(infoName, "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            ServletOutputStream outputStream = response.getOutputStream();

            HorizontalCellStyleStrategy styleBorder =
                    ExcelStyleUtils.createStyleBorder((short) 10, "宋体", (short) 10, "宋体");
            EasyExcel.write(outputStream, JjProgectVo.class)
                    .registerWriteHandler(new JJProgectWriteHandlerColour(list))
                    .registerWriteHandler(new JJProgectWriteHandler(list))
                    .registerWriteHandler(styleBorder)
                    .sheet("基建专家抽取方案").doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CommonException(CommonConstants.ERROR, "基建专家抽取方案导出失败");
        }
    }
}
